export default class Request {
    constructor() {
        // api token
        this.token = 'Cyzz'
        this.baseUrl = 'https://api.baofacai.top'

        // 现在本地测试用这个
        // this.baseUrl = 'http://bfc.api:9911'
        // this.baseUrl = 'http://192.168.0.213:9911'

    }

    request({url, data = {}, method = 'POST'}) {
        return new Promise((resolve, reject) => {

            // flag
            let loadingFlag = true
            setTimeout(() => {
                if (loadingFlag) {
                    uni.showLoading({
                        title: '加载中...'
                    })
                }
            }, 200)

            // console.log(`${this.baseUrl}${url}`)

            uni.request({
                url: `${this.baseUrl}${url}`,
                timeout: 3000,
                method,
                data,
                header: {
                    // 切记, POST这样设置
                    'content-type':'application/x-www-form-urlencoded',
                    // 'content-type': 'application/json',
                    'X-Token-Scan': 'Cyzz',
                },
                success: response => {
                    console.log(response)
                    loadingFlag = false
                    uni.hideLoading()
                    const { data } = response
                    if (data.code === 0 || data.code === -7) {
                        // -7 custom error
                        resolve(data)
                    } else if (data.code === -2) {
                        // not login
                        uni.showToast({
                            title: data.message,
                            icon: 'none',
                            mask: true,
                            duration: 1500
                        })
                        // setTimeout(() => {
                        //     uni.redirectTo({ url: '/sub_user/login/login' })
                        // }, 1200)
                    } else if (data.code === -1) {
                        // 由于有重复请求, 这里不弹了
                        if (data.message != 'error') {
                            uni.showToast({
                                title: data.message || '未知错误',
                                icon: 'none',
                                duration: 500
                            })
                        }
                        reject(data)
                    } else {
                        reject(data)
                    }
                },
                fail: err => {
                    loadingFlag = false
                    uni.hideLoading()
                    // 上线后，可以把这个删除
                    uni.showToast({
                        title: '未知错误',
                        icon: 'error',
                        duration: 500
                    })
                    reject(err)
                },
                complete: () => {
                    loadingFlag = false
                    uni.hideLoading()
                }
            })
        })
    }

    into(data) {
        return this.request({
            url: '/scan/index/into',
            data
        })
    }

    out(data) {
        return this.request({
            url: '/scan/index/out',
            data
        })
    }

	login(data){
		return this.request({
			url:'/scan/index/login',
			data:data
		})
	}
}
